package com.cloud.code.真题;

import java.util.*;

/**
 * 堆栈中的剩余数字
 */
public class Demo13 {
    public static void main(String[] args) {
        List<Integer> list = new LinkedList<>();
        Stack<Integer> stack = new Stack<>();
        List<Integer> numbs = new ArrayList<>(Arrays.asList(170, 5, 10, 20, 50, 85, 1));
        for (int j = 0; j < numbs.size(); j++) {
            int num = numbs.get(j);
            if (list.size() != 0) {
                int sum = 0;
                int count = -1;
                for (int i = list.size() - 1; i >= 0; i--) {
                    sum = sum + list.get(i);
                    if (sum == num) {
                        count = list.size() - i;
                        break;
                    } else if (sum > num) {
                        break;
                    }
                }
                if (count != -1) {
                    int tim = count;
                    while (tim > 0) {
                        stack.pop();
                        tim--;

                    }
                    int size = list.size();
                    for (int i = size - 1; i >= size - count; i--) {
                        list.remove(i);
                    }
                    numbs.add(j + 1, 2 * sum);
                } else {
                    list.add(num);
                    stack.add(num);
                }
            } else {
                list.add(num);
                stack.add(num);
            }
        }
        int size = stack.size();
        for (int i = 0; i < size; i++) {
            System.out.print(stack.pop() + " ");
        }
    }
}
